package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBInVoice implements IFDBInVoice{
    private  Connection con;
    /** Creates a new instance of DBSupplier */
    public DBInVoice() {
      con = DbConnection.getInstance().getDBcon();
    }
    
  //Implements the methods from the interface
    // get all Klasses
    public ArrayList<InVoice> getAllInVoices(boolean retriveAssociation)
    {
    	
        return miscWhere("", retriveAssociation);
    }
    //get one Supplier having the holdId
    public InVoice findInVoice(int InVoiceNo, boolean retriveAssociation)
    {   String wClause = "  InVoiceNo = '" + InVoiceNo + "'";
        return singleWhere(wClause, retriveAssociation);
    }
     

	
	//private methods
    //michWere is used whenever we want to select more than one Supplier 
	private ArrayList<InVoice> miscWhere(String wClause, boolean retrieveAssociation)
	{
            ResultSet results;
	    ArrayList<InVoice> list = new ArrayList<InVoice>();	
		
	    String query =  buildQuery(wClause);
	    System.out.println("print noget ud " +query+"");
            try{ // read the supplier from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 InVoice InVoiceObj = new InVoice();
		 InVoiceObj = buildInVoice(results);	
                 list.add(InVoiceObj);	
		}//end while
                 stmt.close();     
                 
                 
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	//Singelwhere is used when we only select one employee 	
	private InVoice singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		InVoice InVoiceObj = new InVoice();
		
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the klasse from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            InVoiceObj = buildInVoice(results);
                            //assocaition is to be build
                            stmt.close();
                            if(retrieveAssociation)
                            {   //The supervisor and department is to be build as well
                                int InVoiceNo = InVoiceObj.getInVoiceNo();
                                InVoice InVoiceNo1 = singleWhere(" InVoiceNo = '" + InVoiceNo + "'",false);
                                InVoiceObj.setInVoiceNo(InVoiceNo);
                                System.out.println("HoldId selected");
                               // here the department has to be selected as well                                           
                            }
			}
                        else{ //no supplier was found
                            InVoiceObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return InVoiceObj;
	}
	

	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT InVoiceNo, PaymentDate,TotalPriceOfSales FROM InVoice";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	
	//method to build an InVoice object
	private InVoice buildInVoice (ResultSet results)
      {   InVoice InVoiceObj = new InVoice();
          try{ // the columns from the table emplayee  are used
                InVoiceObj.setInVoiceNo(results.getInt("InVoiceNo"));
                InVoiceObj.setPaymentDate(results.getString("PaymentDate"));
                InVoiceObj.setTotalPriceOfSales(results.getInt("TotalPriceOfSales"));
          }
         catch(Exception e)
         {
             System.out.println("error in building the InVoice object");
         }
         return InVoiceObj;
      }

	@Override
	public int deleteInVoice(int InVoiceNo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertInVoice(InVoice iObj, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return 0;
	}

 
}